﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using VivendoByteToolkit.Controls;
using VivendoByteToolkit.Enums;

namespace VivendoByteToolkit.ViewModels
{
    public class CurrencyPickerViewModel : ViewModelBase
    {
        #region Private properties
        decimal originalAmount;
        #endregion

        #region Public properties
        public CurrencyPicker CurrencyPicker { get; set; }

        private decimal amount;
        public decimal Amount
        {
            get
            {
                return amount;
            }
            set
            {
                amount = value;
                this.Integer = ((int)amount).ToString();
                this.Comma = ((int)((amount - (int)amount) * 100)).ToString("00");
                base.RaisePropertyChanged("Amount");
            }
        }

        private string integer;
        public string Integer
        {
            get
            {
                return integer;
            }
            set
            {
                integer = value;
                base.RaisePropertyChanged("Integer");
            }
        }

        private string comma;
        public string Comma
        {
            get
            {
                return comma;
            }
            set
            {
                comma = value;
                base.RaisePropertyChanged("Comma");
            }
        }

        private bool keyboardEnabled;
        public bool KeyboardEnabled
        {
            get
            {
                return keyboardEnabled;
            }
            set
            {
                keyboardEnabled = value;
                base.RaisePropertyChanged("KeyboardEnabled");
            }
        }

        private string symbol;
        public string Symbol
        {
            get
            {
                return symbol;
            }
            set
            {
                symbol = value;
                base.RaisePropertyChanged("Symbol");
            }
        }
        #endregion

        #region Command
        public ICommand IncrementIntegerCommand { get; set; }
        public ICommand DecrementIntegerCommand { get; set; }
        public ICommand SkipUpIntegerCommand { get; set; }
        public ICommand SkipDownIntegerCommand { get; set; }

        public ICommand IncrementCommaCommand { get; set; }
        public ICommand DecrementCommaCommand { get; set; }
        public ICommand SkipUpCommaCommand { get; set; }
        public ICommand SkipDownCommaCommand { get; set; }

        public ICommand EnableKeyboardCommand { get; set; }
        #endregion

        #region ctor
        public CurrencyPickerViewModel()
        {
            this.Amount = 0.0M;
            this.Symbol = "€";
            this.UpdateAmount();

            this.IncrementIntegerCommand = new RelayCommand(IncrementIntegerCommandExecute);
            this.DecrementIntegerCommand = new RelayCommand(DecrementIntegerCommandExecute);
            this.SkipUpIntegerCommand = new RelayCommand(SkipUpIntegerCommandExecute);
            this.SkipDownIntegerCommand = new RelayCommand(SkipDownIntegerCommandExecute);

            this.IncrementCommaCommand = new RelayCommand(IncrementCommaCommandExecute);
            this.DecrementCommaCommand = new RelayCommand(DecrementCommaCommandExecute);
            this.SkipUpCommaCommand = new RelayCommand(SkipUpCommaCommandExecute);
            this.SkipDownCommaCommand = new RelayCommand(SkipDownCommaCommandExecute);

            this.EnableKeyboardCommand = new RelayCommand(EnableKeyboardCommandExecute);
        } 
        #endregion

        #region Integer management
        private void IncrementIntegerCommandExecute()
        {
            var newAmount = this.Amount + 1;

            if (newAmount > CurrencyPicker.GetMaximum(this.CurrencyPicker))
            {
                this.Amount = CurrencyPicker.GetMaximum(this.CurrencyPicker);
            }
            else
            {
                this.Amount = newAmount;
            }

            this.UpdateAmount();
        }

        private void DecrementIntegerCommandExecute()
        {
            var newAmount = this.Amount - 1;

            if (newAmount < CurrencyPicker.GetMinimum(this.CurrencyPicker))
            {
                this.Amount = CurrencyPicker.GetMinimum(this.CurrencyPicker);
            }
            else
            {
                this.Amount = newAmount;
            }

            this.UpdateAmount();
        }

        private void SkipUpIntegerCommandExecute()
        {
            var newAmount = this.Amount + 10;

            if (newAmount > CurrencyPicker.GetMaximum(this.CurrencyPicker))
            {
                this.Amount = CurrencyPicker.GetMaximum(this.CurrencyPicker);
            }
            else
            {
                this.Amount = newAmount;
            }

            this.UpdateAmount();
        }

        private void SkipDownIntegerCommandExecute()
        {
            var newAmount = this.Amount - 10;

            if (newAmount < CurrencyPicker.GetMinimum(this.CurrencyPicker))
            {
                this.Amount = CurrencyPicker.GetMinimum(this.CurrencyPicker);
            }
            else
            {
                this.Amount = newAmount;
            }

            this.UpdateAmount();
        } 
        #endregion

        #region Comma management
        private void IncrementCommaCommandExecute()
        {
            var newAmount = this.Amount + 0.01M;

            if (newAmount > CurrencyPicker.GetMaximum(this.CurrencyPicker))
            {
                this.Amount = CurrencyPicker.GetMaximum(this.CurrencyPicker);
            }
            else
            {
                this.Amount = newAmount;
            }

            this.UpdateAmount();
        }

        private void DecrementCommaCommandExecute()
        {
            var newAmount = this.Amount - 0.01M;

            if (newAmount < CurrencyPicker.GetMinimum(this.CurrencyPicker))
            {
                this.Amount = CurrencyPicker.GetMinimum(this.CurrencyPicker);
            }
            else
            {
                this.Amount = newAmount;
            }

            this.UpdateAmount();
        }

        private void SkipUpCommaCommandExecute()
        {
            var newAmount = this.Amount + 0.10M;

            if (newAmount > CurrencyPicker.GetMaximum(this.CurrencyPicker))
            {
                this.Amount = CurrencyPicker.GetMaximum(this.CurrencyPicker);
            }
            else
            {
                this.Amount = newAmount;
            }

            this.UpdateAmount();
        }

        private void SkipDownCommaCommandExecute()
        {
            var newAmount = this.Amount - 0.10M;

            if (newAmount < CurrencyPicker.GetMinimum(this.CurrencyPicker))
            {
                this.Amount = CurrencyPicker.GetMinimum(this.CurrencyPicker);
            }
            else
            {
                this.Amount = newAmount;
            }

            this.UpdateAmount();
        } 
        #endregion

        private void EnableKeyboardCommandExecute()
        {
            this.KeyboardEnabled = !this.KeyboardEnabled;
        }

        private void UpdateAmount()
        {
            originalAmount = this.Amount;
            this.Integer = ((int)this.Amount).ToString();
            this.Comma = (this.Amount - ((int)this.Amount)).ToString();
            this.UpdateDependencyProperty();
        }

        private void UpdateDependencyProperty()
        {
            if (this.CurrencyPicker != null)
            {
                this.CurrencyPicker.SetValue(CurrencyPicker.AmountProperty, this.Amount);
            }
        }
    }
}